package com.retail.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.retail.common.result.Result;
import com.retail.product.dto.ProductAddDTO;
import com.retail.product.dto.ProductAddResponseDTO;
import com.retail.product.dto.ProductQueryDTO;
import com.retail.product.entity.Product;
import com.retail.product.service.ProductService;
import com.retail.product.vo.ProductVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 商品管理控制器
 *
 * @author retail
 * @since 2024-09-25
 */
@RestController
@RequestMapping("/api/v1/products")
public class ProductController {
    
    private static final Logger log = LoggerFactory.getLogger(ProductController.class);
    
    @Autowired
    private ProductService productService;

    /**
     * 分页查询商品列表
     */
    @GetMapping("/page")
    public Result<IPage<ProductVO>> getProductPage(ProductQueryDTO queryDTO) {
        log.info("获取商品列表请求 - {}", queryDTO);
        
        try {
            Page<ProductVO> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
            IPage<ProductVO> result = productService.getProductPage(page, queryDTO);
            
            log.info("获取商品列表成功，共{}条记录", result.getTotal());
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取商品列表失败", e);
            return Result.error("获取商品列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取商品详情
     */
    @GetMapping("/{id}")
    public Result<ProductVO> getProductById(@PathVariable("id") Long id) {
        log.info("获取商品详情请求 - id: {}", id);
        
        try {
            ProductVO product = productService.getProductById(id);
            log.info("获取商品详情成功 - {}", product);
            return Result.success(product);
        } catch (Exception e) {
            log.error("获取商品详情失败 - id: {}", id, e);
            return Result.error("获取商品详情失败：" + e.getMessage());
        }
    }

    /**
     * 添加新商品
     */
    @PostMapping
    public Result<ProductAddResponseDTO> addProduct(@Valid @RequestBody ProductAddDTO productAddDTO) {
        log.info("添加商品请求 - {}", productAddDTO);
        
        try {
            ProductAddResponseDTO result = productService.addProduct(productAddDTO);
            log.info("添加商品成功 - {}", result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("添加商品失败", e);
            return Result.error("添加商品失败：" + e.getMessage());
        }
    }

    /**
     * 更新商品
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateProduct(@PathVariable("id") Long id, @Valid @RequestBody Product product) {
        log.info("更新商品请求 - id: {}, data: {}", id, product);
        
        try {
            boolean result = productService.updateProduct(id, product);
            log.info("更新商品成功 - id: {}, result: {}", id, result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新商品失败 - id: {}", id, e);
            return Result.error("更新商品失败：" + e.getMessage());
        }
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteProduct(@PathVariable("id") Long id) {
        log.info("删除商品请求 - id: {}", id);
        
        try {
            boolean result = productService.deleteProduct(id);
            log.info("删除商品成功 - id: {}, result: {}", id, result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("删除商品失败 - id: {}", id, e);
            return Result.error("删除商品失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除商品
     */
    @DeleteMapping("/batch")
    public Result<Boolean> deleteProducts(@RequestBody List<Long> ids) {
        log.info("批量删除商品请求 - ids: {}", ids);
        
        try {
            boolean result = productService.deleteProducts(ids);
            log.info("批量删除商品成功 - ids: {}, result: {}", ids, result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("批量删除商品失败 - ids: {}", ids, e);
            return Result.error("批量删除商品失败：" + e.getMessage());
        }
    }

    /**
     * 上架商品
     */
    @PutMapping("/{id}/online")
    public Result<Boolean> onlineProduct(@PathVariable("id") Long id) {
        log.info("上架商品请求 - id: {}", id);
        
        try {
            Product product = new Product();
            product.setStatus(1); // 上架状态
            boolean result = productService.updateProduct(id, product);
            log.info("上架商品成功 - id: {}, result: {}", id, result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("上架商品失败 - id: {}", id, e);
            return Result.error("上架商品失败：" + e.getMessage());
        }
    }

    /**
     * 下架商品
     */
    @PutMapping("/{id}/offline")
    public Result<Boolean> offlineProduct(@PathVariable("id") Long id) {
        log.info("下架商品请求 - id: {}", id);
        
        try {
            Product product = new Product();
            product.setStatus(0); // 下架状态
            boolean result = productService.updateProduct(id, product);
            log.info("下架商品成功 - id: {}, result: {}", id, result);
            return Result.success(result);
        } catch (Exception e) {
            log.error("下架商品失败 - id: {}", id, e);
            return Result.error("下架商品失败：" + e.getMessage());
        }
    }
}
